<script>
$j(function() { load_category(); });
function reload_category() {
	$j('.error').html('');
	load_category();
}
function load_category() {
	var cat_id = $j('#categories :selected').val();
	$j('#parent').val(cat_id);
	load_position(cat_id);
	load_subcat(cat_id);
}
function edit(id) {
	var title = $j('#cat-' + id).html();
	$j('#cat-' + id).html('<input type="text" class="textfield rounded" value="' + title + '" id="title-cat-' + id + '" /> <input type="button" class="button rounded" value="Update" onClick="update(' + id + ')" /> <input type="button" class="button rounded" value="Cancel" onClick="cancel(' + id + ')" />');
}
function cancel(id) {
	$j.ajax({
		url: '<?=base_url()?>admin/store/getcat/',
		type: 'POST',
		data: ({id:id}),
		dataType: "html",
		success: function(html) {
			$j('#cat-' + id).html(html);
		}
	})	
}
function update(id) {
	var title = $j('#title-cat-' + id).val();
	$j.ajax({
		url: '<?=base_url()?>admin/store/updatecat/',
		type: 'POST',
		data: ({id:id,title:title}),
		dataType: "html",
		success: function(html) {
			$j('#cat-' + id).html(title);
		}
	})	
}
function move(step,id) {	
	window.location = '<?=base_url()?>admin/store/movecat/' + id + '/' + step;
}
function delcat(id) {
	if(confirm('Are you sure you want to delete this sub category?')) {
		window.location = '<?=base_url()?>admin/store/delcat/' + id;
	}
}
function load_position(cat_id) {
	$j.ajax({
		url: '<?=base_url()?>admin/store/getsubcatlist/',
		type: 'POST',
		data: ({id:cat_id}),
		dataType: "html",
		success: function(html) {
			$j("#position").html(html);
		}
	})
}
function load_subcat(cat_id) {
	$j.ajax({
		url: '<?=base_url()?>admin/store/getsubcat/',
		type: 'POST',
		data: ({id:cat_id}),
		dataType: "html",
		success: function(html) {
			$j("#subcat").html(html);
		}
	})
}
</script>
    	<div class="left">
        	<h1>Store Management</h1>
            <div class="bar">

            	<div class="text">Manage Categories</div>
            	<div class="cr"></div>
            </div>
            <div class="box">
            	<p>
                	Please select the category you want to manage &nbsp;
                    <select id="categories" onchange="reload_category()">
                    	<?php foreach($main_categories as $mc) { ?>
                        <option value="<?=$mc['id']?>"<?php if($mc['id'] == $this->session->userdata('parent_category')) print ' selected="selected"'; ?>><?=$mc['title']?></option>
                        <?php } ?>
                    </select>
                </p>
            </div>            
            <hr />
            <div class="box bgw">
            	<form method="post" action="<?=base_url()?>admin/store/category/add">
                <input type="hidden" name="parent_id" id="parent" />
            	<h3>Add new Sub category</h3>
                <dl class="one"><dt>Title</dt><dd><input type="text" class="textfield rounded" name="title" /></dd>
                	<dd class="error"><?=$this->session->flashdata('error_input')?></dd>
                </dl>
                <dl class="one"><dt>Position</dt><dd id="position">
                	<select name="order">
                    	<option value="0">At the end</option>
                    </select>
                </dd></dl>
                <dl class="one"><dt>&nbsp;</dt>
                	<dd><input type="submit" class="button rounded" value="Add" /></dd></dl>
                <dl></dl>
                </form>
            </div>
            <hr />
            <div class="box">
            	<h3>Categories List</h3>
                <div class="row-title">
                	<div class="cat-name">Category name</div>
                    <div class="cat-func">Down</div>
                    <div class="cat-func">Up</div>
                    <div class="cat-func">Delete</div>
                    <div class="cat-func">Edit</div>
                </div>
                <div id="subcat">
                
                </div>
            </div>
        </div>
        
